package com.ruoyi.factory.qylock.controller.api;

import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.factory.qylock.service.InfoSynService;
import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import com.ruoyi.factory.qylock.utils.UicodeBackslashU;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName InfoSynController
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2023/5/9 0009 13:51
 **/
@EnableScheduling
@Controller("infoSynController")
public class InfoSynController {

    @Autowired
    private InfoSynService infoSynService;

    /**
     * @author: Liujiaji
     * @date: 2023/4/25 0025 14:10
     * @description: TODO 员工信息同步
     * @param: []
     * @return: void
     **/
    public void staff(){
        //循环3次
        for (int i = 0; i < 5; i++) {
            infoSynService.staff(String.valueOf(i+1));
        }

    }

    public static void main(String[] args) {

        JSONObject json = new JSONObject();
        JSONObject json2 = new JSONObject();
        json2.put("name","新乡市瑞丰新材料股份有限公司");
        json.put("company",json2);
        String s = json.toString();
        String url = "/v2/category/group";
        String res = OkHttpUtils.okHttpPostqys(url,s);
        String s1 = UicodeBackslashU.unicodeToCn(res);
        JSONObject jsonObject = JSONObject.fromObject(s1);
        JSONObject result = jsonObject.getJSONObject("result");
        JSONArray children = result.getJSONArray("children");

        JSONArray liuc = new JSONArray();
        String type = "Z3ZY";
        if(!children.isEmpty()) liuc = findliuc (children, type);
        Object o = liuc.get(0);

        List<String> list = new ArrayList<>();
        if(null != liuc && !liuc.isEmpty()){
            String s2 = liuc.toString();
            System.out.println(s2);
        }else{
            System.out.println("失败");
        }
    }

    public static JSONArray findliuc (JSONArray jsa, String type) {
        for (Object child : jsa) {
            JSONObject jsonObjects = JSONObject.fromObject(child);
            String name = jsonObjects.getString("name");
            if (name.contains(type)) {
                return jsonObjects.getJSONArray("categoryList");
            } else {
                JSONArray children = null;
                try {
                    children = jsonObjects.getJSONArray("children");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (children != null && !children.isEmpty()) {
                    JSONArray res = findliuc(children, type);
                    if(null != res && !res.isEmpty()){
                        return findliuc(children, type);
                    }
                }
            }
        }
        return null;
    }
}
